package org.hq.client;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
import org.hq.client.ws.OkWebSocket;
import org.java_websocket.client.WebSocketClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/")
public class Root {
    private WebSocket webSocket=null;
    private OkHttpClient client=null;
    private WebSocketClient sc=null;
    @GetMapping
    public String getById(){
        if(webSocket==null){
            this.okclient();
        }



       // this.javaclient();


        return "Hello World";
    }

    private void okclient() {

        client = new OkHttpClient();
        Request.Builder builder = new Request.Builder().url("ws://192.168.2.253:8080/myWS");
        builder.addHeader("userid","heqiao");
        builder.addHeader("password","1813033378");
        Request request = builder.build();
        webSocket = client.newWebSocket(request, new OkWebSocket());

    }

    private void javaclient() {
        try {
            if (sc==null){
                // "ws://example.controller/path?param1=value1&param2=value2"
                URI  uri = new URI("ws://192.168.2.253:8080/myWS");
                Map<String,String> map = new HashMap<>();
                map.put("userid","heqiao");
                map.put("password","1813033378");
                sc=new SocketClient(uri,map);
                sc.connect();
            }

        } catch (URISyntaxException e) {

        }
    }
}
